/**数据验证*/
var PermissionValidator = function() {  
    var handleSubmit = function() { 
        $('.form-horizontal').validate({  
            errorElement : 'span',  
            errorClass : 'help-block',  
            focusInvalid : false,  
            rules : {  
            	"permission.pname" : {  
                    required : true  
                },  
                "permission.purl" : {  
                    required : true  
                },
                "permission.seq"  :{
                	required : true,
                	digits:true
                }
            },  
            messages : {  
            	"permission.pname" : {  
                    required : "请输入权限名称."  
                },  
                "permission.purl" : {  
                    required : "请输入链接地址."  
                },
                "permission.seq"  :{
                	required : "请输入顺序号",
                	digits:"请输入整数（例如：1，2，3）"
                }
            },  
  
            highlight : function(element) { 
                $(element).closest('.control-group').addClass('error');  
            },  
  
            success : function(label) { 
                label.closest('.control-group').removeClass('error');  
                label.remove();
            },  
  
            errorPlacement : function(error, element) {  
            	element.parent('div').append(error);  
            },  
  
            submitHandler : function(form) {
                form.submit();
            }  
        });  
  
        $('.form-horizontal input').keypress(function(e) {  
            if (e.which == 13) {  
                if ($('.form-horizontal').validate().form()) {  
                    $('.form-horizontal').submit();  
                }  
                return false;  
            }  
        });  
    }  
    return {  
        init : function() { 
            handleSubmit(); 
        }  
    };  
  
}();  
/**设置界面树形菜单*/
var setting = {
	view: {
		selectedMulti: false
	},
	edit: {
		enable: true,
		showRemoveBtn: false,
		showRenameBtn: false
	},
	data: {
		keep: {
			parent:true,
			leaf:true
		},
		simpleData: {
			enable: true
		}
	}
};
var	newCount=1;
function add(e) {
	var zTree = $.fn.zTree.getZTreeObj("menup");
	var nodes = zTree.getSelectedNodes();
	var isParent = true;
	if(nodes!=null && nodes.length>0){
	    isParent=false;
	}
	var treeNode = nodes[0];
	if (treeNode) {
		$("#fid").val(treeNode.id);
		treeNode = zTree.addNodes(treeNode, {id:("T"+100 + newCount), pId:treeNode.id, isParent:isParent, name:"功能名称"});
	} else {
		$("#fid").val("");
		treeNode = zTree.addNodes(null, {id:("T"+100 + newCount), pId:0, isParent:isParent, name:"功能名称"});
	}
	if (treeNode) {
		zTree.editName(treeNode[0]);
	} else {
		alert("系统只支持两级菜单，无法继续添加");
	}
};
function edit() {
	var zTree = $.fn.zTree.getZTreeObj("menup"),
	nodes = zTree.getSelectedNodes(),
	treeNode = nodes[0];
	if (nodes.length == 0) {
		alert("请选择要修改的菜单");
		return;
	}
	$("#pname").val(treeNode.name);
	$("#fid").val(treeNode.pId);
	$("#pid").val(treeNode.id);
	$("#purl").val(treeNode.purl);
	$("#seq").val(treeNode.seq);
	$("#style").val(treeNode.style);
};
/*删除数据*/
function remove(e) {
	var zTree = $.fn.zTree.getZTreeObj("menup"),
	nodes = zTree.getSelectedNodes(),
	treeNode = nodes[0];
	if (nodes.length == 0) {
		alert("请先选择功能");
		return;
	}
	if(treeNode.id.indexOf("T")==0){
		var callbackFlag = $("#callbackTrigger").attr("checked");
		zTree.removeNode(treeNode, callbackFlag);
		return;
	}
	var pid=treeNode.id;
	$.post("../system/remove_Permission.action",{"pid":pid}, function(result) {
		if(result.msg=="succ"){
			$('#myModal').modal('hide');
			$("#alertinfo").html("权限删除成功");
			$('#myinfo').modal('show');
			window.location.reload();
		}else{
			alert(result.msg);
			$('#myModal').modal('hide');
			$("#alertinfo").html(result.msg);
			$('#myinfo').modal('show');
		}
	});
};


/**保存数据*/
function savePermission(){
	$('#myModal').modal('show');
	var pid=$("#pid").val();
	var pname=$("#pname").val();
	var purl=$("#purl").val();
	var seq=$("#seq").val();
	var fid=$("#fid").val();
	var style=$("#style").val();
	$.post("../system/save_Permission.action",{"permission.pid":pid,"permission.pname":pname,
		"permission.purl":purl,"permission.seq":seq,"permission.parent.pid":fid,"permission.style":style}, function(result) {
		if(result.msg=="succ"){
			$('#myModal').modal('hide');
			$("#alertinfo").html("权限维护成功");
			$('#myinfo').modal('show');
			window.location.reload();
		}else{
			$('#myModal').modal('hide');
			$("#alertinfo").html(result.msg);
			$('#myinfo').modal('show');
		}
	});
};
$(document).ready(function(){
	PermissionValidator.init();
    $.post("../system/loadAll_Permission.action",{},function(result){
    	$.fn.zTree.init($("#menup"), setting, result.menus);
    });
    $.post("../system/loadParent_Permission.action",{},function(result){
    	$.each(result.permissions,function(i,permission){
    		$("#fid").append("<option value='"+permission.pid+"'>"+permission.pname+"</option>");
    	});
    });
	$("#addLeaf").bind("click", add);
	$("#remove").bind("click", remove);
	$("#edit").bind("click", edit);
});
